<?php
namespace app\model;
use think\Model;
use think\model\concern\SoftDelete;

class Menu extends Model{
    protected $autoWriteTimestamp = true;
    use SoftDelete;


    public function children(){
        return $this->hasMany(Menu::class,'parent_id');
    }

    //嵌套输出超级管理员
    public static function nestedCategories($parentId = 0)
    {
        return self::with(['children'])->where([['parent_id','=', $parentId],['state','=','1']])->select()->each(function ($item) {
            if ($item->children) {
                $item->children = self::nestedCategories($item->id);
            }
        });
    }
    //嵌套输出普通用户
    public static function nestedUser($parentId = 0)
    {
        return self::with(['children'])->where([['parent_id','=', $parentId],['id','<>','5'],['state','=','1']])->select()->each(function ($item) {
            if ($item->children) {
                $item->children = self::nestedUser($item->id);
            }
        });
    }
}